package com.hdax.dm.listener;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

@RabbitListener(queues = {"dmsms"})
@Component
public class RabbitMQListener {

    @Value("${tencent.sms.accessKeyId}")
    private String accessKeyId;
    @Value("${tencent.sms.accessKeySecret}")
    private String accessKeySecret;
    @Value("${tencent.sms.signName}")
    private String signName;
    @Value("${tencent.sms.templateCode}")
    private String templateCode;

    @RabbitHandler
    public void sendSms(Map<String,String> params) throws Exception{
        String phoneNumber = params.get("phoneNumber");
        String vcode = params.get("vcode");
        try{
            Credential cred = new Credential(accessKeyId, accessKeySecret);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            com.tencentcloudapi.sms.v20210111.SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);

            com.tencentcloudapi.sms.v20210111.models.SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {"+86"+phoneNumber};
            req.setPhoneNumberSet(phoneNumberSet1);

            req.setSmsSdkAppId("1400505117");
            req.setSignName(signName);
            req.setTemplateId(templateCode);

            String[] templateParamSet1 = {""+vcode};
            req.setTemplateParamSet(templateParamSet1);

            com.tencentcloudapi.sms.v20210111.models.SendSmsResponse resp = client.SendSms(req);

            System.out.println(SendSmsResponse.toJsonString(resp));
            System.out.println("执行成功！");
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }
}
